/**
 * Created by ashin on 2017/10/17.
 */
(function () {

    var orderId = $j.GetQueryString('orderId');

    function init() {
        renderData();
        bindBtnEvent();
    }

    function renderData() {
        $.showPreloader('加载中');
        $j.restPost('/order/doSelect', {orderId: orderId}).done(function (mess, data) {
            if (data.params != "安装") {
                $('#installControl').hide();
                $('#seeDate').parents('li').hide();
                $('#validityDate').parents('li').hide();
                $('#other').css('display','block');
                $('#other').html(data.params + '时间设置');
                $('#other').data('id',data.params);
            } else {
                $('#seeDate').html(data.seeDate == undefined ? '尚未设置' : $j.getTargetFullDate(data.seeDate));
                $('#validityDate').html(data.validityDate == undefined ? '尚未设置' : $j.getTargetFullDate(data.validityDate));
            }
            $('#installDate').html(data.installDate == undefined ? '尚未设置' : $j.getTargetFullDate(data.installDate));
            $('#orderId').html(data.orderId);
            $('#nickName').html(data.name);
            $('#phone').html(data.phone);
            $('#deviceName').html(data.deviceName);
            $('#params').html(data.params);
            $('#type').html(data.type);
            $('#createTime').html($j.getTargetFullDate(data.createTime));
            $.hidePreloader();
        }).fail(function (mess) {
            $.toast(mess);
            $.hidePreloader();
        });

    }

    function bindBtnEvent() {

        //勘察时间
        $('#seeDateModel').on('click', function () {
            openDateModel(function (seeDate) {
                updateDate({
                    orderId:orderId,
                    seeDate:seeDate
                });
            },'seeDateInput');
        });

        //安装时间
        $('#installDateModel').on('click', function () {
            openDateModel(function (installDate) {
                updateDate({
                    orderId:orderId,
                    installDate:installDate,
                    type:'已安装'
                });
            },'installDateInput');
        });

        //有效期设置
        $('#validityDateModel').on('click', function () {
            openDateModel(function (validityDate) {
                updateDate({
                    orderId:orderId,
                    validityDate:validityDate
                });
            },'validityDateInput');
        });

        //有效期设置
        $('#other').on('click', function () {

            var type = $(this).data('id');

            openDateModel(function (installDate) {
                var param = {
                    orderId:orderId,
                    installDate:installDate,
                    type:'已' + type
                }

                updateDate(param);
            },'otherDateInput');
        });

        var openDateModel = function (callback,id) {
            $.modal({
                title: '日期选择',
                text: '<input type="text" id="' + id + '" class="datetime-picker"/>',
                buttons: [
                    {
                        text: '确定',
                        bold: true,
                        onClick: function () {
                            callback($('#' + id).val());
                        }
                    },
                    {
                        text: '取消'
                    }
                ]
            });

            var myDate = new Date();

            $(".datetime-picker").off().datetimePicker({
                value: [myDate.getFullYear(), myDate.getMonth() + 1, myDate.getDate(), myDate.getHours(), myDate.getMinutes()]
            });

        };

        var updateDate = function (param) {
            $.showPreloader('设置中');
            $j.restPost('/order/doUpdate', param).done(function (mess, data) {
                $.hidePreloader();
                renderData();
            }).fail(function (mess) {
                $.toast(mess);
                $.hidePreloader();
            })
        }

    }

    init();
})(jQuery)